package javasec.contempt.actions;

import java.io.File;

import javasec.contempt.consoles.SeedLogConsole;
import javasec.contempt.data.SeedConnector;
import javasec.contempt.dialogs.KeyWindow;
import javasec.contempt.dialogs.StartSeedDialog;
import javasec.contempt.dialogs.TextWindow;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;



public class StartSeedAction extends Action implements ISelectionListener,
		IWorkbenchAction
{
	private final IWorkbenchWindow window;
	public final static String ID="javasec.contempt.StartServerAction";
	//private IStructuredSelection selection;
	
	public StartSeedAction(IWorkbenchWindow window)
	{
		this.window=window;
		setId(ID);
		setText("Start Seed");
		setToolTipText("santa43622");
		
		window.getSelectionService().addSelectionListener(this);
	}
	
	public void selectionChanged(IWorkbenchPart part, ISelection selection)
	{
		
	}

	public void dispose()
	{
		window.getSelectionService().removeSelectionListener(this);
	}

	public void run()
	{
		IWorkbenchPage page = window.getActivePage();
		
		StartSeedDialog dialog=new StartSeedDialog(null);
		dialog.open();
		
		//StartSeedDialog dialog=new StartSeedDialog(null);
		//dialog.open();
		
		/*
		File seedServerFile=new File("SeedServer.jar");
		
		if(!seedServerFile.exists())
		{
			seedServerFile=new File(System.getProperty("user.home")+
				File.separator+"javasec.contempt"+File.separator+"SeedServer.jar");
		}
		
		if(!seedServerFile.exists())
		{
			System.out.println("error: could not find SeedServer.jar");
			return;
		}

		Process seedProcess=null;
		try
		{
			Runtime.getRuntime().exec(new String[]{"gksu","mkdir /root/.seed/"});
			Runtime.getRuntime().exec(new String[]{"gksu","mv workspace/seed.key workspace/seed-trusted.key /root/.seed/"});
			seedProcess=Runtime.getRuntime().exec(new String[] {"gksu","java -jar "+seedServerFile.getPath()});
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		// open the log console
		SeedLogConsole console=new SeedLogConsole("seed log console",null);
		console.setStream(seedProcess.getInputStream());
		
		ConsolePlugin.getDefault().getConsoleManager().addConsoles(
			new IConsole[]{console});
		*/
	}
}
